


sub median {
 my $ref_list = $_[0];

#>>> computes the length of the passed array 

my $len = $#$ref_list + 1;
#>>> sort the parameter array 
 
@list = sort { $a <=> $b } @$ref_list;

 #>>compute the median
if ( $len % 2 == 1)
{
return $list[$len / 2];
}
else 
{
return ($list[$len / 2] + $list[$len / 2 - 1]) / 2;
}
}
  
#>>>>>>>>begin the main program 
#>>> create two test arrays one odd length and one even length 

@list1 = (11,36,5,20,41,6,8,0,9);
@list2 = (43,77,11,29,8,51,9,18);

#>>>>>>calling median on both arrays and display the results 

$med = median(\@list1);
print "the median of the first array is :$med \n";
$med = median(\@list2);
print " the median of the second array is : $med \n ";
